﻿using NetDh.Lib;
using NetDh.Lib.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;

namespace NetDh.Presentation.Common
{
    public class ApiActionFilterAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// 是否可匿名访问(不做身份验证)
        /// </summary>
        public bool AllowAnonymous { get; set; }
        /// <summary>
        /// Action是否判断Role角色
        /// </summary>
        public string Role { get; set; }
        //用于调度追踪同一个Action，OnActionExecuting进来的次数。
        //->
        public string TmpTest { get; set; }
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            //api的身份验证是根据Header中的token
            var ticket = actionContext.Request.GetHeaderValue("token");
            var token = TokenHelper.GetToken(ticket);
            void funcNoAuthenticatedResult(bool signOut = false)
            {
                token = UserToken.CreateEmptyInstance();
                if (AllowAnonymous) return;
                //Response 赋值，则会自动中断请求返回Response。
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, "Request Not Authorized..");
            }
            if (token == null)
            {
                funcNoAuthenticatedResult();
            }
            token._HttpTime = DateTime.Now;
            actionContext.Request.Properties[TokenHelper.TOKEN_NAME] = token;
            //ActionArguments是Action的函数参数，是可以用于传递变量到OnActionExecuted(actionExecutedContext.ActionContext.ActionArguments)
            //ActionArguments["bb"]=1，那么如果Action有函数参数bb，那么它的值就为1了。
            //actionContext.ActionArguments[SlowHttpTracer.TRACERT_KEY] = DateTime.Now;

            //注意 HttpContext.Current在异步Action中有可能为null。目前未使用异步Action
            //HttpContext.Current.Items[SlowHttpTracer.TRACERT_KEY] = tracer;
            base.OnActionExecuting(actionContext);//基类是空函数
        }
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            //var tracer = HttpContext.Current.Items[SlowHttpTracer.TRACERT_KEY] as SlowHttpTracer;
            object curToken;
            actionExecutedContext.Request.Properties.TryGetValue(TokenHelper.TOKEN_NAME, out curToken);
            if (curToken != null)
            {
                SlowHttpTracer.CheckLog(curToken as UserToken, WebPlatform.api);
            }
            //actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(HttpStatusCode.Unauthorized, "api OnActionExecuted");
            base.OnActionExecuted(actionExecutedContext);//基类是空函数
        }
        public override Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
        {
            return base.OnActionExecutedAsync(actionExecutedContext, cancellationToken);
        }
        public override Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
        {
            return base.OnActionExecutingAsync(actionContext, cancellationToken);
        }
    }
}
